package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//菜品模块
@Service //service层
@Transactional// 开启事务
public class DishServiceImpl implements DishService {

    @Autowired // 注入
    private DishMapper dishMapper;

    @Autowired// 注入
    private DishFlavorMapper dishFlavorMapper;

    @Autowired// 注入
    private CategoryMapper categoryMapper;

    @Override     // 查询所有分页
    public Page<Dish> findByPige(Integer pageNum, Integer pageSize, String name) {
       //1.构建菜品条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(name),Dish::getName,name);
        // 2.开启分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        // 3.分页查询
        page = dishMapper.selectPage(page, wrapper);
        //  4.遍历菜品列表
        /*
      根据前端需求我们得知 我们需要展现的除了菜品的名称以外
      还有他是属于哪个口味的也要体现出来，还有就是他是属于哪个菜系也要体现出来这样就用到了三张表
      DIsh 菜品表  dishflavor口味表   category菜系表 我们遍历一张表的同时 去查询另一张表 在遍历 嵌套遍历查询
         */

        List<Dish> records = page.getRecords();// 遍历
        for (Dish record : records) {
            // 这个遍历出来就是菜品 再根据菜品去查询口味  新建lambda 建立条件
            LambdaQueryWrapper<DishFlavor> df = new LambdaQueryWrapper<>();
            df.eq(DishFlavor::getDishId,record.getId());
            // 在调用口味的mapper去查询
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(df);
            //在封装口味到菜品中
            record.setFlavors(dishFlavorList);
            // 在调用菜系的mapper 去查询   根据DIsh 菜品表中的CategoryId 去比对category菜系表的id 就能知道他是那个菜系
            Category category = categoryMapper.selectById(record.getCategoryId());
            // 封装菜系进菜品中
            record.setCategoryName(category.getName());

        }
        //     5.返回对象
    return page;
    }
    //新增
    @Override
    public void save(Dish dish) {
        // 新增
       dishMapper.insert(dish);
       // 遍历口味
        for (DishFlavor flavor : dish.getFlavors()) {
            // 关联菜品id
            flavor.setDishId(dish.getId());
            // 新增口味
            dishFlavorMapper.insert(flavor);
        }
    }
    // 回显
    @Override
    public Dish findById(Long id) {
        // 先查菜品
        Dish dish = dishMapper.selectById(id);
        //根据菜品id查口味
        LambdaQueryWrapper<DishFlavor> df = new LambdaQueryWrapper<>();
        df.eq(DishFlavor::getDishId,id);
        // 调用mapepr 查询
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(df);
        // 将口味设置到菜品中
        dish.setFlavors(dishFlavors);
        // 返回菜品
        return dish;
    }
    // 修改菜品
    @Override
    public void update(Dish dish) {
        // 1.修改菜品基本信息
        dishMapper.updateById(dish);
        // 2.根据菜品id删除原来口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(qw);
        // 3.遍历新增口味
     for (DishFlavor flavor : dish.getFlavors()) {
            // 关联菜品id
            flavor.setDishId(dish.getId());
            // 口味新增
            dishFlavorMapper.insert(flavor);
        }
    }



    // 禁用启动
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        // 条件对象
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        // 修改实体
        Dish dish = new Dish();
        dish.setStatus(status);
        // 调用mapper
        dishMapper.update(dish, wrapper);
    }
    //删除
    @Override
    public void delete(List<Long> ids) {
        //创建条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        // 根据条件查询
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //在遍历得出每一个id
        for (Dish dish : dishList) {
            if (dish.getStatus()==1){
                throw new CustomException("在售菜品不允许删除");
            }
            // 先删菜品
            dishMapper.deleteBatchIds(ids);
            // 再删除口味
            LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
            // 确定要删除的条件
            qw.in(DishFlavor::getDishId,ids);
            //删除
            dishFlavorMapper.delete(qw);
        }
    }


    //查询所有菜品
    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
       //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);//对比id
        qw.eq(Dish::getStatus,status); //对比状态码
        List<Dish> dishList = dishMapper.selectList(qw); //根据项目查询
        for (Dish dish : dishList) {
            /// 封装口味
            LambdaQueryWrapper<DishFlavor> eq = new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(eq);
            dish.setFlavors(dishFlavors);
        }
        // 返回结果
        return dishList;
    }
    // 回显
    @Override
    public List<Dish> findByList(Long categoryId,String name) {
        // 构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //
        qw.like(StrUtil.isNotBlank(name),Dish::getName,name);
        qw.eq(categoryId!=null,Dish::getCategoryId,categoryId);// 根据分类查询
        qw.eq(Dish::getStatus,1); //起售状态
        return dishMapper.selectList(qw);

    }


}
